package com.jeethink.erp.weixin.controller;

import com.jeethink.common.core.controller.BaseController;
import com.jeethink.common.core.domain.AjaxResult;
import com.jeethink.common.core.domain.entity.SysUser;
import com.jeethink.common.core.domain.entity.TbShop;
import com.jeethink.common.core.domain.model.LoginUser;
import com.jeethink.common.utils.uuid.UUID;
import com.jeethink.erp.commodity.domain.TbCommodity;
import com.jeethink.erp.commodity.service.ITbCommodityService;
import com.jeethink.erp.shop.service.ITbShopService;
import com.jeethink.erp.utils.R;
import com.jeethink.erp.weixin.form.LoginForm;
import com.jeethink.erp.weixin.service.WeiXinLoginService;
import com.jeethink.framework.web.service.TokenService;
import com.jeethink.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Set;

@Api("微信调用接口")
@RestController
@RequestMapping("/weixin/shop")
public class WeiXinController extends BaseController {

    @Autowired
    private ITbShopService tbShopService;

    @Autowired
    private WeiXinLoginService weiXinLoginService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ITbCommodityService tbCommodityService;

    /**
     * 查询店铺树结构列表
     */
    @ApiOperation("获取店铺信息")
    @GetMapping("/list")
    public AjaxResult list(TbShop tbShop)
    {
        List<TbShop> tbShops = tbShopService.selectTbShopList(tbShop);
        return AjaxResult.success(tbShops);
    }

    @PostMapping("/login")
    @ApiOperation("小程序登陆系统")
    public R login(@Valid @RequestBody LoginForm form){

        String uuid = getUUid();
        LoginUser loginUser = weiXinLoginService.login(form.getUserName(),form.getPassword(),uuid);
        // 生成token
        String token = tokenService.createToken(loginUser);
        return R.ok("登陆成功").put("token",token).put("user",loginUser.getUser());
    }

    @GetMapping("/searchUserSummary")
    @ApiOperation("小程序获取当前登录用户数据")
    public R searchUserSummary(Long userId){
        SysUser user = sysUserService.selectUserById(userId);
        return R.ok().put("user",user);
    }

    @GetMapping("/findStockList")
    @ApiOperation("小程序获取我的库存信息")
    public R findStockList(Long shopId,int pageCurrent,int pageSize){
        TbCommodity tbCommodity = new TbCommodity();
        TbCommodity tbCommodity2 = new TbCommodity();
        pageCurrent = (pageCurrent - 1) * pageSize;
        tbCommodity.setPageCurrent(pageCurrent);
        tbCommodity.setPageSize(pageSize);
        tbCommodity.setShopId(shopId);
        tbCommodity2.setShopId(shopId);
        List<TbCommodity> getMineStockList = tbCommodityService.mineStock(tbCommodity);
        List<TbCommodity> getMineStockList2 = tbCommodityService.mineStock(tbCommodity2);
        return R.ok().put("getMineStockList",getMineStockList).put("total",getMineStockList2.size());
    }



    private static String getUUid(){
        String uuid = "";

        UUID uuid1 = UUID.randomUUID();
        UUID uuid2 = UUID.randomUUID();
        uuid = uuid1.toString() + uuid2.toString();
        uuid = uuid.replaceAll("-", "").substring(0, 32);
        return uuid;
    }
}
